package com.starblink.wishlist.search.ui

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.apollographql.apollo3.api.Optional
import com.blankj.utilcode.util.GsonUtils
import com.starblink.android.basic.base.BaseViewModel
import com.starblink.android.basic.config.StorageKeys
import com.starblink.android.basic.network.base.SkGqlClient
import com.starblink.basic.apollo.ext.optional
import com.starblink.basic.storage.SkStorage
import com.starblink.rocketreserver.SearchProductsQuery
import com.starblink.rocketreserver.type.ProductQueryParam
import kotlinx.coroutines.launch
import java.util.*

class WishlistSearchVM : BaseViewModel() {

    //        var mList: MutableLiveData<ArrayList<Any>> = MutableLiveData()
    val history = LinkedList<String>()
    val historyLD = MutableLiveData<Boolean>()
    var curKeyword = ""
    var merchantWebNo = ""

    fun queryHistory() {
        history.clear()
        GsonUtils.fromJson(
            SkStorage.getString(
                "${StorageKeys.SEARCH_WISHLIST_HISTORY}_$merchantWebNo",
                ""
            ), history.javaClass
        )
            ?.let {
                history.addAll(it)
                historyLD.postValue(history.isNotEmpty())
            }
    }

    fun saveHistory(word: String) {
        history.remove(word)
        history.add(0, word)
        while (history.size >= 16) {
            history.removeLast()
        }
//        historyLD.postValue(true)
        SkStorage.put(
            "${StorageKeys.SEARCH_WISHLIST_HISTORY}_$merchantWebNo",
            GsonUtils.toJson(history)
        )
    }

    fun clearHistory() {
        history.clear()
        historyLD.postValue(false)
        SkStorage.remove("${StorageKeys.SEARCH_WISHLIST_HISTORY}_$merchantWebNo")
    }


    fun initRefresh() {

    }


}